
import {formatTime} from "@/utils/utils";

/**
 * 判断水位下注
 * @param hgaData
 * @param psData
 * @param amount
 * @param min_odds
 * @param max_odds
 * @param count_odds
 * @param ps_min_odds
 * @param ps_max_odds
 */
function betAmount (hgaData, psData, amount = 100, min_odds = 0.97, max_odds = 99, count_odds = 1.97, ps_min_odds = 0.909, ps_max_odds = 99) {
	let hga_h_odds = hgaData['odds_h']*1;
	let hga_c_odds = hgaData['odds_c']*1;


	let ps_h_odds = psData['odds_h'] * 1;
	let ps_c_odds = psData['odds_c'] * 1;

	if(hga_h_odds >= min_odds && hga_h_odds + ps_c_odds < count_odds && hga_h_odds <= max_odds && ps_c_odds >= ps_min_odds && ps_c_odds <= ps_max_odds ) {
		return {
			'hga_c_amount': 0,
			'hga_h_amount': amount,
			'bet_count_odds': (hga_h_odds + ps_c_odds).toFixed(3),
		}
	}
	if(hga_c_odds >= min_odds && hga_c_odds + ps_h_odds < count_odds && hga_c_odds <= max_odds  && ps_h_odds >= ps_min_odds && ps_h_odds <= ps_max_odds ) {
		return {
			'hga_h_amount': 0,
			'hga_c_amount': amount,
			'bet_count_odds': (hga_c_odds + ps_h_odds).toFixed(3),
		}
	}
	return false;
}

/**
 *
 * 执行中单逻辑检测
 *
 * @param hga_game 黄瓜游戏列表
 * @param ps_game 平博游戏列表
 * @param nameGuanTeam 已关联的比赛列表
 * @param config 缓存配置
 */
export function diff_game(hga_game = [], ps_game = [], nameGuanTeam = {}, config = {}){

	let ps_game_key_list = [];
	ps_game.forEach((item) => {
		let competition = item['competition'];
		let one_key = `${competition['time_str']}_${competition['team_h']}`;
		let one_key2 = `${competition['time_str']}_${competition['team_c']}`;
		ps_game_key_list[one_key] = item;
		ps_game_key_list[one_key2] = item;
	})


	let keys = ['concede_points', 'min_big', 'r_concede_points', 'r_min_big'];


	let readyBetList = [];
	let betDataList = [];

	for(let i=0;i<hga_game.length;i++) {
		let item = hga_game[i];
		let competition = item['competition'];
		let one_key = `${competition['time_str']}_${competition['team_h']}`;
		let one_key2 = `${competition['time_str']}_${competition['team_c']}`;

		/// 没有对应的平博关联 key 直接跳过
		let ps_game_key = nameGuanTeam[one_key]||nameGuanTeam[one_key2]||"";
		if(!ps_game_key) {
			continue;
		}
		/// 没有对应的平博 游戏数据 直接跳过
		let ps_game_info = ps_game_key_list[ps_game_key]||"";
		if(!ps_game_info) {
			continue;
		}

		/// 遍历 让球和大小球逻辑
		for (let j=0;j<keys.length;j++) {
			let key = keys[j];
			let _itemHgaData = item[key]??"";
			let _itemPsData = ps_game_info[key]??"";

			/// 没有盘口跳过
			if(!_itemHgaData || !_itemPsData) {
				continue;
			}
			/// 没水位跳过
			if(!_itemHgaData['odds_h'] || !_itemPsData['odds_h'] || !_itemHgaData['odds_c'] || !_itemPsData['odds_c']) {
				continue;
			}

			// 比分不同也不打
			if(competition['score_h_c'] !== ps_game_info['competition']['score_h_c']) {
				continue;
			}

			let bet_amount = (config['bet_amount'] < 50 ? 50 : config['bet_amount']);

			if(competition['diff_time_type'] === 'doing') {
				bet_amount = (config['doing_bet_amount'] < 50 ? 50 : config['doing_bet_amount']);
			}


			if(config['bet_random_max_number'] && config['bet_random_amount']) {
				let random_number = Math.floor(Math.random() * (config['bet_random_max_number']*1 + 1 ));
				let random_amount = random_number * config['bet_random_amount'];
				bet_amount -= random_amount;
				if(bet_amount <50) {
					bet_amount = 50;
				}
			}

			let temp_data = false;
			let hasDoing = competition['diff_time_type'] === 'doing';

			/// 赛前
			if(!hasDoing) {
				temp_data = betAmount(_itemHgaData, _itemPsData, bet_amount, config['hga_odds_min'], config['hga_odds_max'], config['bet_count_odds'], config['ps_odds_min'], config['ps_odds_max']);
			}else {
				/// 滚球
				temp_data = betAmount(_itemHgaData, _itemPsData, bet_amount, config['doing_hga_odds_min'], config['doing_hga_odds_max'], config['doing_bet_count_odds'], config['doing_ps_odds_min'], config['doing_ps_odds_max']);
			}
			/// 不满足水位跳过
			if(!temp_data) {
				continue;
			}

			/// 原始数据
			temp_data['raw_data'] = item;

			temp_data['hga_id'] = competition['gid'];
			temp_data['ps_id'] = ps_game_info['competition']['gid'];
			temp_data['hga_team'] = competition;
			temp_data['ps_team'] = ps_game_info['competition'];
			temp_data['type'] = key;
			temp_data['diff_time'] =  competition['diff_time']??"";
			temp_data['diff_time_type'] =  competition['diff_time_type']??"";

			temp_data['hga_odds_data'] = _itemHgaData;
			temp_data['ps_odds_data'] = _itemPsData;

			temp_data['concede_points'] = item['concede_points']??{};
			temp_data['r_concede_points'] = item['r_concede_points']??{};
			temp_data['min_big'] = item['min_big']??{};
			temp_data['r_min_big'] = item['r_min_big']??{};
			temp_data['create_time'] = +new Date();
			temp_data['bet_time'] = formatTime();

			if(temp_data['hga_h_amount'] > 0) {
				temp_data['odds'] = _itemHgaData['odds_h'];
				temp_data['bet_hdp'] = _itemHgaData['betHdp_h'];

				temp_data['bet_type_str'] = key.indexOf('concede_points') !== -1 ? competition['team_h'] : "大";

				temp_data['bet_data'] = _itemHgaData['buy_data']['h'];
				if(temp_data['diff_time_type'] === 'warn') {
					temp_data['bet_data'] = _itemHgaData['buy_data']['warn_h'];
				}

				temp_data['bet_amount'] = temp_data['hga_h_amount'];

				temp_data['ps_odds'] = _itemPsData['odds_c'];

			}else {
				temp_data['odds'] = _itemHgaData['odds_c'];
				temp_data['bet_hdp'] = _itemHgaData['betHdp_c'];

				temp_data['bet_type_str'] = key.indexOf('concede_points') !== -1 ? competition['team_c'] : "小";

				temp_data['bet_data'] = _itemHgaData['buy_data']['c'];
				if(temp_data['diff_time_type'] === 'warn') {
					temp_data['bet_data'] = _itemHgaData['buy_data']['warn_c'];
				}

				temp_data['bet_amount'] = temp_data['hga_c_amount'];

				temp_data['ps_odds'] = _itemPsData['odds_h'];

			}

			temp_data['bet_data']['golds'] = temp_data['bet_amount'];

			readyBetList.push(temp_data);
		}



		readyBetList.forEach((_item) => {
			let type = _item['type'];
			let hasType = "";

			/// 受球和让球
			if(['concede_points','r_concede_points'].indexOf(type) !== -1) {
				let isTempHdp = false;
				if(_item['bet_hdp']*1 !== 0 ) {
					isTempHdp = true;
				}
				if(isTempHdp) {
					if(_item['bet_hdp'] > 0) {
						/// 受球
						hasType = "ball";
					}else {
						/// 让球
						hasType = "let";
					}
				}
			}

			/// 小球和大球
			if(['min_big', 'r_min_big'].indexOf(type) !== -1) {
				if(_item['hga_c_amount'] > 0) {
					hasType = "min";
				}else {
					hasType = "big";
				}
			}

			let hga_team = _item['hga_team'];

			let hasDoing = hga_team['diff_time_type'] === 'doing';

			/// 当前盘口
			let itemBetHdp = _item['bet_hdp'];

			/// 当前 大小球 值
			let minBigBetHdp = _item['min_big']['betHdp_h'];

			/// 当前让球值
			let concedePointsBetHdp = Math.abs(_item['concede_points']['betHdp_h']);


			let hasBetHdpAlike = false;
			let ps_odds_data = _item['ps_odds_data'];
			let hga_odds_data = _item['hga_odds_data'];

			/// 所以让球和大小球都相同就中单入库
			if(ps_odds_data['betHdp_h'] == ps_odds_data['betHdp_c'] && ps_odds_data['betHdp_h'] == hga_odds_data['betHdp_h'] && ps_odds_data['betHdp_h'] == hga_odds_data['betHdp_c']) {
				hasBetHdpAlike = true;
			}else {
				/// 判断让球相同再入库
				if(ps_odds_data['betHdp_h'] == hga_odds_data['betHdp_h'] && ps_odds_data['betHdp_c'] == hga_odds_data['betHdp_c']) {
					hasBetHdpAlike = true;
				}
			}
			if(!hasBetHdpAlike) {
				return;
			}

			let oneKey = "";

			/// 平赔率较低一方
			let hasLowOdds = false;
			if(_item['hga_h_amount'] > 0) {
				/// 投主
				if(ps_odds_data['odds_h'] < ps_odds_data['odds_c']) {
					hasLowOdds = true;
				}
			}else {
				/// 投客
				if(ps_odds_data['odds_h'] > ps_odds_data['odds_c']) {
					hasLowOdds = true;
				}
			}

			/// 正在进行的比赛，但是不属于正常比赛
			if(_item['diff_time'] === -9999999) {
				return;
			}

			/// 水位差低于0.05不中单
			let odds_diff = Math.abs(ps_odds_data['odds_h'] - ps_odds_data['odds_c']);
			odds_diff = odds_diff.toFixed(3) * 1;

			/// 队伍带中的，有些麻烦，要先站点数据库先获取再匹配
			let isCenter = false;
			let ballHagCenter = false;

			// 受球 不买客队
			if(hasType === 'ball') {
				if(_item['hga_c_amount'] > 0 && !isCenter) {
					ballHagCenter = true;
				}
			}

			/***测试代码***/
			// console.log(hasType);
			// hasLowOdds = true;
			/***测试代码***/

			/// 平博水位差 配置对比
			let ps_odds_diff_status = false;
			if(!hasDoing) {
				/// 赛前
				if(odds_diff >= config.ps_diff_odds_min && odds_diff < config.ps_diff_odds_max) {
					ps_odds_diff_status = true;
				}
			}else {
				/// 滚球
				if(odds_diff >= config.doing_ps_diff_odds_min && odds_diff < config.doing_ps_diff_odds_max) {
					ps_odds_diff_status = true;
				}
			}


			///受球和小球
			if(hasLowOdds && ps_odds_diff_status && hasType && ['ball', 'min'].indexOf(hasType) !== -1) {
				/// 受球
				if(hasType === 'ball') {
					_item['is_ready'] = 2;
					oneKey = `${_item['type']}_${hasType}`;
				}
				/// 小球
				if(hasType === 'min') {
					if(concedePointsBetHdp <= 2.5) {
						_item['is_ready'] = 2;
						oneKey = `${_item['type']}_${hasType}`;
					}
				}
			}
			///让球和大球
			if(hasLowOdds && hasType && ps_odds_diff_status  && ['let', 'big'].indexOf(hasType) !== -1) {
				/// 让球
				if(hasType === 'let') {
					if(minBigBetHdp >= 2.5) {
						_item['is_ready'] = 2;
						oneKey = `${_item['type']}_${hasType}`;
					}
				}
				if(hasType === 'big') {
					_item['is_ready'] = 2;
					oneKey = `${_item['type']}_${hasType}`;
				}
			}

			/// 满足下单条件，入库再
			if(_item['is_ready'] === 2) {

				let has_bet_type = config['bet_type_list'].filter((bet_type) => {
					return bet_type['checked'] && bet_type['type'] === `${_item['type']}_${hasType}`;
				})
				if(has_bet_type.length < 1) {
					/// 虽然有中单，但是不打这个类型
					// console.log([`${hga_team['team_h']} VS ${hga_team['team_c']} ${oneKey}`, "虽然有中单，但是不打这个类型"])
					return;
				}


				oneKey += "_" + hga_team['diff_time_type'];

				_item['odds_diff'] = odds_diff;
				_item['has_type'] = hasType;
				_item['one_key'] = `${_item['hga_id']}_${oneKey}`;
				betDataList.push(_item);
			}

		})
	}

	return betDataList;

}